(function() {
    var defaults = {
        //renderTo: $(document.body),
        autoInit: false,
        autoDestory: false
    }, callback = Tgm.callback;
    var Fixed = function(opts) {
        this.id = Tgm.id();
        $.extend(this, defaults, opts);
        var obj = $.extend({}, this);
        this.get = function(key) {
            return obj[key];
        };
        this.set = function(key, value) {
            return obj[key] = value;
        };
        if (this.autoInit) {
            this.init();
        }
    };
    Fixed.prototype = (function() {
        var helper = {
            init: {
                action: function() {
                    var t = this;
                    helper.init.html.call(t);
                    helper.init.node.call(t);
                    helper.init.style.call(t);
                    helper.init.event.call(t);
                },
                html: function() {
                },
                node: function() {
                },
                style: function() {
                },
                event: function() {
                }
            },
            render: {
                action: function() {
                    var t = this, r = t.get('renderTo'), win = $(window), o = r.offset(), top, left, st = win.scrollTop(), sl = win.scrollLeft(), ot = o.top, ol = o.left, mt = parseInt(r.css('margin-top')) || 0, ml = parseInt(r.css('margin-left')) || 0;
                    if (Tgm.ie6) {
                        r.css({
                            top: ot - mt,
                            left: ol - ml,
                            position: 'absolute'
                        });
                        helper.render.event.call(t);
                    } else {
                        top = (st > 0 ? ot - st : ot) - mt;
                        left = (sl > 0 ? ol - sl : ol) - ml;
                        r.css({
                            top: top,
                            left: left,
                            position: 'fixed'
                        });
                    }
                },
                event: function() {
                    var t = this;
                    if (Tgm.ie6) {
                        helper.ie6.fixed.call(t);
                    }
                }
            },
            unrender: {
                action: function() {
                    var t = this, r = t.get('renderTo'), win = $(window), o = r.offset(), top, left, mt = parseInt(r.css('margin-top')) || 0, ml = parseInt(r.css('margin-left')) || 0;
                    r.css({
                        top: o.top - mt,
                        left: o.left - ml,
                        position: 'absolute'
                    });
                    helper.unrender.event.call(t);
                },
                event: function() {
                    var t = this;
                    if (Tgm.ie6) {
                        helper.ie6.unfixed.call(t);
                    }
                }
            },
            destory: {
                action: function() {
                    var t = this;
                }
            },
            ie6: (function() {
                var scrollFns = {};
                return {
                    fixed: function() {
                        var t = this, r = t.get('renderTo'), win = $(window), wt = win.scrollTop(), wl = win.scrollLeft(), o = r.offset(), ot = o.top, ol = o.left, mt = parseInt(r.css('margin-top')) || 0, ml = parseInt(r.css('margin-left')) || 0;
                        scrollFns[t.get('id')] = function() {
                            r.css({
                                top: ot + win.scrollTop() - wt - mt,
                                left: ol + win.scrollLeft() - wl - ml
                            });
                        };
                        $(window).bind('scroll', scrollFns[t.get('id')]);
                    },
                    unfixed: function() {
                        var t = this;
                        $(window).unbind('scroll', scrollFns[t.get('id')]);
                        delete scrollFns[t.get('id')];
                    }
                }
            })(),
            rule: {}
        };
        return {
            constructor: Fixed,
            init: function() {
                if (this.get('isInit')) {
                    return this;
                } else {
                    if (callback(this.get('bi'), this.get('bip'), this.get('bis') || this) !== false) {
                        helper.init.action.apply(this);
                        this.set('isInit', true);
                        callback(this.get('ai'), this.get('aip'), this.get('ais') || this);
                        return this;
                    }
                    return false;
                }
            },
            render: function() {
                if (!this.get('isInit')) {
                    this.init();
                }
                if (this.get('isRender')) {
                    return this;
                } else {
                    if (callback(this.get('br'), this.get('brp'), this.get('brs') || this) !== false) {
                        helper.render.action.apply(this);
                        this.set('isRender', true);
                        callback(this.get('ar'), this.get('arp'), this.get('ars') || this);
                    }
                }
            },
            unrender: function() {
                if (this.get('isRender')) {
                    if (callback(this.get('bc'), this.get('bcp'), this.get('bcs') || this) !== false) {
                        helper.unrender.action.apply(this);
                        this.set('isRender', false);
                        callback(this.get('ac'), this.get('acp'), this.get('acs') || this);
                        if (this.get('autoDestory')) {
                            this.destory();
                        }
                        return this;
                    }
                } else {
                    return false;
                }
            },
            destory: function() {
                if (this.get('isInit')) {
                    if (callback(this.get('bd'), this.get('bdp'), this.get('bds') || this) !== false) {
                        helper.destory.action.apply(this);
                        this.set('isInit', false);
                        callback(this.get('ad'), this.get('adp'), this.get('ads') || this);
                        return this;
                    }
                } else {
                    return false;
                }
            }
        };
    })();
	Tgm.reg('Fixed', Fixed);
})();
